package com.atguigu.ssyx.sys.controller;


import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.model.sys.Region;
import com.atguigu.ssyx.model.sys.RegionWare;
import com.atguigu.ssyx.sys.service.IRegionWareService;
import com.atguigu.ssyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonAlias;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 城市仓库关联表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/admin/sys/regionWare")
@Api(tags = "开通区域接口")
//@CrossOrigin
@RequiredArgsConstructor
public class RegionWareController {

    private final IRegionWareService regionWareService;

    /*    url: `${api_name}/${page}/${limit}`,
        method: 'get',
        params: searchObj*/
    @ApiOperation("分页查询开通区域列表")

    @GetMapping("/{page}/{limit}")
    public Result pageList(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "regionWareQueryVo", value = "查询对象", required = false) RegionWareQueryVo regionWareQueryVo
    ) {

        //1.创建分页对象
        Page<RegionWare> p = new Page<>(page, limit);

        //2.创建分页条件
        LambdaQueryWrapper<RegionWare> queryWrapper = new LambdaQueryWrapper<>();


        //根据区域名称或者仓库名称进行模糊查询
        if (!StringUtils.isEmpty(regionWareQueryVo.getKeyword())) {
            queryWrapper.like(RegionWare::getRegionName, regionWareQueryVo.getKeyword())
                    .or()
                    .like(RegionWare::getWareName, regionWareQueryVo.getKeyword());
        }

        //3.调用业务层分页查询
        regionWareService.page(p, queryWrapper);


        return Result.ok(p);
    }


    //1.查询所有仓库信息
    //2.根据区域关键字查询地区信息
    //3.把选择的仓库和区域添加到region_ware表中

    /*    url: `${api_name}/save`,
        method: 'post',
        data: role*/
    @ApiOperation("新增开通区域")
    @PostMapping("/save")
    public Result save(
            @RequestBody @ApiParam(name = "regionWare", value = "开通区域对象", required = true) RegionWare regionWare
    ) {

//        把选择的仓库和区域添加到region_ware表中
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        //根据区域id查询region_ware表中是否有记录
        //一个区域只能开通一个仓库 也就是开通区域就是开通仓库
        wrapper.eq(RegionWare::getRegionId, regionWare.getRegionId());
        int count = regionWareService.count(wrapper);
        if (count > 0) {
            throw new SsyxException(ResultCodeEnum.REGION_OPEN);
        }

        regionWareService.save(regionWare);
        return Result.ok(null);
    }


    /*  url: `${api_name}/remove/${id}`,
      method: 'delete'*/
    @ApiOperation("根据id删除开通区域")
    @DeleteMapping("/remove/{id}")
    public Result remove(
            @ApiParam(name = "id", value = "开通区域ID", required = true) @PathVariable Long id
    ) {
        regionWareService.removeById(id);
        return Result.ok(null);
    }


    /* url: `${api_name}/updateStatus/${id}/${status}`,
     method: 'post'*/
    @ApiOperation(value = "取消开通区域")
    @PostMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        regionWareService.updateStatus(id, status);
        return Result.ok(null);
    }

}
